package org.istt.web;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.istt.domain.Category;
import org.istt.domain.SubCategory;
import org.istt.service.CategoryManager;
import org.istt.service.SubCategoryManager;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;

/**
 * @author Thond
 *
 */
public class SubCategoryAction extends BaseAction<SubCategoryManager, SubCategory, Long> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private SubCategoryManager subCategoryManager;
	
	@Autowired
	private CategoryManager categoryManager;
	
	private List<Category> categories;
	
	private List<SubCategory> subCategories;
	
	private String name;
	
	private String cateId;
	public String getCateId() {
		if (entity != null) {
			if(this.cateId==null) cateId = entity.getCategory() == null ? selDefKey : entity.getCategory().getId() + "";
		}
		return cateId;
	}
	
	@Override
	public SubCategory getEntity() {
		SubCategory tmpSubCategory = new SubCategory();
		
		tmpSubCategory.setId(getId());
		tmpSubCategory.setName(name);
		
		if(StringUtils.isNotEmpty(cateId)){
			Category cate = this.categoryManager.get(Long.parseLong(cateId));
			tmpSubCategory.setCategory(cate);
		}
		return tmpSubCategory;
	}

	public void setCateId(String cateId) {
		this.cateId = cateId;
	}

	private String cancel;
	private String delete;
	
	public String getCancel() {
       return this.cancel;
    }
	
	public void setCancel(String cancel) {
        this.cancel = cancel;
    }

    public void setDelete(String delete) {
        this.delete = delete;
    }
    
    public String getDelete() {
		return delete;
	}
	
	@Override
	public String execute() {
        if (cancel != null) {
            return "cancel";
        }
        
        if (delete != null) {
            return "delete";
        }
        
        return SUCCESS;
    }
	
	
	@Override
	public Long getId() {
		return (entity==null? id : entity.getId());//id
	}
	public boolean isAllowDelete(){
		boolean isValid = true;
		if(entity!=null){
			if(entity.getFiles().size()!=0) isValid = false;
		}
		return isValid;
	}


	public boolean isValid() {
		return mgr.isUnique(new String[]{"name"}, new String[]{entity.getName()}, entity.getId());
	}

	public String remove(){
		if(getId()!=null){
			mgr.remove(getId());
		}
		return SUCCESS;
	}


//	@Override
//	public String save() {
//		if (log.isDebugEnabled()) {
//            log.debug("entering 'save' method");
//        }
//        if(StringUtils.isNotEmpty(cateId)){
//        	Category selectedCategory = this.categoryManager.get(Long.parseLong(cateId));
//        	
//        	entity.setCategory(selectedCategory);
//        	
//        	((SubCategoryManager)mgr).save(entity);
//        	
//        	setSaveMessage();
//        }
//                
//        return SUCCESS;
//	}
	

	@Override
	protected void setDeleteMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("categoryType.deleted"));
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	@Override
	protected void setSaveMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("categoryType.saved"));
	}

	public void setSubCategoryManager(SubCategoryManager subCategoryManager) {
		this.subCategoryManager = subCategoryManager;
	}

	public SubCategoryManager getSubCategoryManager() {
		return subCategoryManager;
	}

	public void setCategoryManager(CategoryManager categoryManager) {
		this.categoryManager = categoryManager;
	}

	public CategoryManager getCategoryManager() {
		return categoryManager;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public List<Category> getCategories() {
/*//		List<Category> lstcategories = this.categoryManager.getSortedList();
//		for (Iterator<Category> iterator = lstcategories.iterator(); iterator.hasNext();) {
//			Category category = (Category) iterator.next();
//			category.setCategoryName(category.getCategoryName());
//		}
*/		
		return this.categoryManager.getSortedList();
	}

	public void setSubCategories(List<SubCategory> subCategories) {
		this.subCategories = subCategories;
	}

	public String getListSubCateByCategory(){
		
		return SUCCESS;
	}
	
	public List<SubCategory> getSubCategories() {
		try {
			if(StringUtils.isNotEmpty(cateId)){
				return this.subCategoryManager.findByCategoryId(Long.parseLong(cateId));
			}else{
				List<Category> lstCate = getCategories();
				if(lstCate.size()!=0){
					this.cateId = lstCate.get(0).getId()+"";
					return new ArrayList<SubCategory>(lstCate.get(0).getSubCategories());
				}
				else return new ArrayList<SubCategory>();
			}
		} catch (Exception e) {
			e.printStackTrace();
			
			return new ArrayList<SubCategory>();
		}
		
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		if(entity!=null){
			name = entity.getName();
		}
		return name;
	}
		
}
